import { routerRedux } from 'dva/router';
import { fakeRegister } from '../services/user';
import { setAuthority } from '../utils/authority';

export default {
  namespace: 'register',

  state: {
    status: undefined,
  },

  effects: {
    *submit({ payload = {} }, { call, put }) {
      const response = yield call(fakeRegister, payload);
      if (response.status) {
        localStorage.setItem('userno', response.data.sno || response.data.tno);
        yield put({
          type: 'registerHandle',
          payload: response,
        });
        yield put(
          routerRedux.push({
            pathname: '/user/register-result',
            state: {
              account: response.data.sno || response.data.tno,
            },
          })
        );
      }
    },
  },

  reducers: {
    registerHandle(state, { payload }) {
      setAuthority(payload.data.role);
      return {
        ...state,
        status: payload.status,
      };
    },
  },
};
